//
//  NewFeatureViewController.swift
//  Weibo
//
//  Created by 付启刚 on 15/12/16.
//  Copyright © 2015年 faq. All rights reserved.
//

import UIKit
import SnapKit
private let imageCount=4
private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    init(){
        let layout=UICollectionViewFlowLayout()
        super.init(collectionViewLayout: layout)
        //设置大小
        layout.itemSize=UIScreen.mainScreen().bounds.size
        //滚动方向
        layout.scrollDirection = .Horizontal
        //        间距
        layout.minimumInteritemSpacing=0
        layout.minimumLineSpacing=0
        //隐藏滚动条
        collectionView?.showsHorizontalScrollIndicator=false
        //分页
        collectionView?.pagingEnabled=true
        //取消弹簧效果
        collectionView?.bounces = false
    }
    //默认手写代码 使用xib/nib将报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)as!NewFeatureCell
        cell.index=indexPath.item
        
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //cell完成展示之后 indexPath 对应的上一个cell
        //获取准确的cell
        let cell=collectionView.visibleCells().last! as! NewFeatureCell
        //获取cell的indexPath
        let index=collectionView.indexPathForCell(cell)!
        //当index.item=3时,显示按钮并执行动画
        if index.item==imageCount-1{
            cell.showAnimation()
        }
        
    }
}


class NewFeatureCell: UICollectionViewCell {
    var index:Int = 0{
        didSet{//重写set方法
            IconImage.image=UIImage(named: "new_feature_\(index + 1)")
            //隐藏开始按钮
            startBtn.hidden=true
        }
    }
    @objc private func startBtnDidClick() {
        print(__FUNCTION__)
        //动画结束后 发出页面切换的通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    }
    private func showAnimation(){
        //动画开始之前 修改btn大小,改为0
        startBtn.transform=CGAffineTransformMakeScale(0, 0)
        //显示startBtn
        startBtn.hidden=false
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5.0, options: [], animations: { () -> Void in
            self.startBtn.transform=CGAffineTransformIdentity//闭包中需要加self.   回到最初大小
            }) { (_) -> Void in
                print("动画执行完成")
        }
    }
    //重写构造方法 cell构造的入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //默认为手写代码 使用xib将报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置界面
    private func setupUI(){
        //添加到contentView
        contentView.addSubview(IconImage)
        contentView.addSubview(startBtn)
        //使用snapkit进行自动布局
        //设置新特性图片的约束
        IconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)//make指需要添加约束的对象
        }
        //设置开始按钮的约束
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        //添加按钮点击事件
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    }
    //分析所有视图中的子视图控件 对所有视图控件进行懒加载
    lazy var IconImage:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    lazy var startBtn:UIButton={
        let btn=UIButton()
        btn.setTitle("开始体验", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.sizeToFit()
        return btn
    }()
    
}

